﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IPCommon;
using LibraryUS;
using LibraryDS;
using IPData;
using IPUserService;

namespace LibraryMain
{
    public partial class f001_CM_DM_LOAI_TD_DE : Form
    {
        public f001_CM_DM_LOAI_TD_DE()
        {
            InitializeComponent();
            format_controls();
        }

        #region Public Interface
        public void display()
        {
            this.ShowDialog();
        }
        public void display_for_update(US_CM_DM_LOAI_TD ip_us_cm_dm_loai_td)
        {
            m_us_cm_dm_loai_td = ip_us_cm_dm_loai_td;
            m_e_form_mode = DataEntryFormMode.UpdateDataState;
            this.ShowDialog();
        }
        public void display_for_insert()
        {
            m_e_form_mode = DataEntryFormMode.InsertDataState;
            this.ShowDialog();
        }

        #endregion

        #region Members

        DataEntryFormMode m_e_form_mode;
        US_CM_DM_LOAI_TD m_us_cm_dm_loai_td = new US_CM_DM_LOAI_TD();
        #endregion

        #region Data Structure
        #endregion

        #region Private Method
        private void format_controls()
        {
            CControlFormat.setFormStyle(this);
            set_define_events();
        }
        private void us_object_to_form()
        {
            m_txt_ma_loai.Text = m_us_cm_dm_loai_td.strMA_LOAI;
            m_txt_ten_loai.Text = m_us_cm_dm_loai_td.strTEN_LOAI;
        }
        private void form_to_us_object()
        {
            m_us_cm_dm_loai_td.strTEN_LOAI = m_txt_ten_loai.Text;
            m_us_cm_dm_loai_td.strMA_LOAI = m_txt_ten_loai.Text;
        }
        private void set_inital_form_load()
        {
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.UpdateDataState:
                    us_object_to_form();
                    break;
                case DataEntryFormMode.InsertDataState:
                    break;
                default:
                    break;
            }
        }
        private void set_define_events()
        {
            this.Load += new EventHandler(f001_CM_DM_LOAI_TD_DE_Load);
            m_cmd_update.Click += new EventHandler(m_cmd_update_Click);
            m_cmd_exit.Click += new EventHandler(m_cmd_exit_Click);
        }
        private bool check_validate_data_is_ok()
        {
            if (!CValidateTextBox.IsValid(m_txt_ma_loai, DataType.StringType, allowNull.NO, true))
            {
                return false;
            }
            if (!CValidateTextBox.IsValid(m_txt_ma_loai, DataType.StringType, allowNull.NO, true))
            {
                return false;
            }
            return true;
        }
        private void save_data()
        {
            if (check_validate_data_is_ok() == false) return;
            form_to_us_object();
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.UpdateDataState:
                    m_us_cm_dm_loai_td.Update();
                    break;
                case DataEntryFormMode.InsertDataState:
                    m_us_cm_dm_loai_td.Insert();
                    break;
                default:
                    break;
            }
            BaseMessages.MsgBox_Infor("Dữ liệu đã được cập nhât!");
            this.Close();
        }

        #endregion

        #region Event
        private void f001_CM_DM_LOAI_TD_DE_Load(object sender, EventArgs e)
        {
            try
            {
                set_inital_form_load();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        private void m_cmd_update_Click(object sender, EventArgs e)
        {
            try
            {
                save_data();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        private void m_cmd_exit_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        #endregion
    }
}
